package com.siwei.apply.controller;

import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.res.ProjectCycleRes;
import com.siwei.apply.domain.res.ProjectOverviewRes;
import com.siwei.apply.domain.vo.IdsVo;
import com.siwei.apply.domain.vo.ProjectFilterVo;
import com.siwei.apply.domain.vo.ProjectUpdateVo;
import com.siwei.apply.domain.vo.ProjectVo;
import com.siwei.apply.service.ProjectService;
import com.siwei.common.core.domain.R;
import com.siwei.common.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目控制器
 */
@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController {
    @Autowired
    ProjectService projectService;

    /**
     * 添加项目
     *
     * @param projectVo
     * @return
     */
    @PostMapping("")
    public R<Map<String, String>> Add(@RequestBody ProjectVo projectVo) {
        try {
            String id = projectService.add(projectVo);
            Map<String, String> map = new HashMap<>();
            map.put("id", id);
            return R.ok(map);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取项目
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Project> Get(@PathVariable String id) {
        try {
            Project project = projectService.get(id);
            return R.ok(project);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取项目列表
     *
     * @return
     */
    @PostMapping("/list")
    public R<Map<String, Object>> GetList(@RequestBody ProjectFilterVo projectFilterVo) {
        try {
            Map<String, Object> projects = projectService.getList(projectFilterVo);
            return R.ok(projects);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取项目概览
     */
    @GetMapping("/overview")
    public R<ProjectOverviewRes> GetOverview(@RequestParam(required = false) Integer projectType) {
        try {
            ProjectOverviewRes res = projectService.getOverview(projectType);
            return R.ok(res);
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 获取项目周期（仅定义接口，逻辑未实现）
     */
    @GetMapping("/cycle/{projectId}")
    public R<List<ProjectCycleRes>> GetCycle(@PathVariable String projectId) {
        List<ProjectCycleRes> projectCycleRes = projectService.getCycle(projectId);
        return R.ok(projectCycleRes);
    }

    /**
     * 更新项目
     *
     * @param projectUpdateVo
     * @return
     */
    @PutMapping("")
    public R<Void> Update(@RequestBody ProjectUpdateVo projectUpdateVo) {
        try {
            projectService.update(projectUpdateVo);
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除项目
     *
     * @param idsVo 包含要删除的项目ID列表
     * @return
     */
    @DeleteMapping("")
    public R<Void> Delete(@RequestBody IdsVo idsVo) {
        try {
            if (idsVo == null || idsVo.getIds() == null || idsVo.getIds().isEmpty()) {
                return R.fail("删除的项目ID列表不能为空");
            }
            projectService.batchDelete(idsVo.getIds());
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}
